<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-28 11:35:09
 * @Last Modified time: 2023-02-22 17:40:25
 */
namespace app\common\model\store;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use think\model\concern\SoftDelete;

use app\common\enum\EnumFactory;
use app\common\model\store\GoodsCateModel;
use app\common\model\store\GoodsCateRelModel;
use app\common\model\store\GoodsLabelModel;
use app\common\model\store\GoodsLabelRelModel;
use app\common\model\store\GoodsSkuModel;
use app\common\model\store\GoodsServerModel;
use app\common\model\store\GoodsServerRelModel;

use frank\utils\helper\ArrayHelper;
/**
 * 商品
 * Class GoodsModel
 * @package app\common\model\store
 */
class GoodsModel extends BaseModel
{
    use ModelTrait;
    use SoftDelete;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'store_goods';

    protected $deleteTime = 'delete_time';

    protected $defaultSoftDelete = 0;

    // 追加字段
    protected $append = [
        'goods_image'
    ];

    /**
     * 关联商品分类中间模型
     */
    public function cateRel(){
        return $this->hasMany(GoodsCateRelModel::class,'goods_id','id');
    }

    /**
     * 关联商品分类
     */
    public function cate(){
        return $this->belongsToMany(GoodsCateModel::class,GoodsCateRelModel::class,'cate_id','goods_id');
    }

    /**
     * 关联商品标签中间模型
     */
    public function labelRel(){
        return $this->hasMany(GoodsLabelRelModel::class,'goods_id','id');
    }

    /**
     * 关联商品标签
     */
    public function label(){
        return $this->belongsToMany(GoodsLabelModel::class,GoodsLabelRelModel::class,'label_id','goods_id');
    }
    /**
     * 关联商品服务中间模型
     */
    public function serverRel(){
        return $this->hasMany(GoodsServerRelModel::class,'goods_id','id');
    }

    /**
     * 关联商品服务
     */
    public function server(){
        return $this->belongsToMany(GoodsServerModel::class,GoodsServerRelModel::class,'server_id','goods_id');
    }

    /**
     * 关联商品规格
     */
    public function sku(){
        return $this->hasMany(GoodsSkuModel::class,'goods_id','id');
    }

    /**
     * 状态修改器
     */
    public function getGoodsStatusAttr($value)
    {
        return EnumFactory::instance('store.goods.status')->getItem($value);
    }

    /**
     * 规格类型修改器
     */
    public function getSpecTypeAttr($value)
    {
        return EnumFactory::instance('store.goods.spec_type')->getItem($value);
    }

    /**
     * 规格类型修改器
     */
    public function getDeliveryShippingAttr($value)
    {
        return EnumFactory::instance('store.delivery_shipping')->getItem($value);
    }

    /**
     * 商品图片设置器
     */
    public function setGoodsImgsAttr($value){
        if(is_array($value)){
            return implode(',', $value);
        }
        return $value;
    }

     /**
     * 商品图片追加器
     */
    public function getGoodsImageAttr($value,$data){
        if(!empty($data['goods_imgs']) && !is_array($data['goods_imgs'])){
            return explode(',', $data['goods_imgs'])[0];
        }
        return '';
    }
    
     /**
     * 商品销量追加器
     */
    public function getGoodsSalesAttr($value,$data){
        return ($data['sales_initial'] ?? 0) + ( $data['sales_actual'] ?? 0);
    }
    

    /**
     * 配送方式设置器
     */
    public function setDeliveryTypeAttr($value){
    	if(is_array($value)){
    		return implode(',', $value);
    	}
    	return $value;
    }

    /**
     * 商品图片获取器
     */
    public function getGoodsImgsAttr($value){
        if(!empty($value)){
            return explode(',', $value);
        }
        return $value;
    }

    /**
     * 配送方式获取器
     */
    public function getDeliveryTypeAttr($value){
        if(!empty($value)){
            return explode(',', $value);
        }
        return [];
    }

    /**
     * 回收站搜索器
     */
    public function searchDestroyAttr($query, $value)
    {
        $query->onlyTrashed();
    }

    /**
     * id搜索器
     */
    public function searchIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('id', $value);
        } else {
            $query->where('id', $value);
        }
    }

    /**
     * goods_name搜索器
     */
    public function searchGoodsNameAttr($query, $value)
    {
        $query->where('goods_name', 'like','%'.$value.'%');
    }

    /**
     * 分类搜索器
     */
    public function searchCateIdAttr($query, $value)
    {
        if ($value) {
            if (is_array($value)) {
                $query->whereIn('id', function ($query) use ($value) {
                    $query->name('store_goods_cate_rel')->where('cate_id', 'IN', $value)->field('goods_id')->select();
                });
            } else {
                $query->whereFindInSet('cate_id', $value);
            }
        }
    }
}
